package com.xyazm.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.xyazm.user.domain.entity.UserInfoDO;
import com.xyazm.user.domain.entity.UserMenuDO;
import com.xyazm.user.domain.entity.UserRoleDO;
import com.xyazm.user.domain.vo.UserInfoVO;
import com.xyazm.user.mapper.UserInfoMapper;
import com.xyazm.user.mapper.UserMenuMapper;
import com.xyazm.user.mapper.UserRoleMapper;
import com.xyazm.user.service.IUserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class UserServiceImpl implements IUserService {

    @Resource
    private UserInfoMapper userInfoMapper;
    @Resource
    private UserMenuMapper userMenuMapper;
    @Resource
    private UserRoleMapper userRoleMapper;

    @Override
    public UserInfoVO getUserInfo(String username) {
        UserInfoVO userInfoVo = new UserInfoVO();
        LambdaQueryWrapper<UserInfoDO> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(UserInfoDO::getUsername,username);
        // 根据用户账号查询用户信息
        UserInfoDO userInfoDO = userInfoMapper.selectOne(queryWrapper);
        if (userInfoDO == null) {
            throw new RuntimeException("userInfo is null");
        }
        userInfoVo.setIcon(userInfoDO.getIcon());
        userInfoVo.setUsername(userInfoDO.getUsername());
        // 根据后台用户ID获取菜单
        List<UserMenuDO> menuList = userMenuMapper.getMenuList(userInfoDO.getId());
        userInfoVo.setMenus(menuList);
        // 根据后台用户ID获取角色
        List<UserRoleDO> roleList = userRoleMapper.getRoleList(userInfoDO.getId());
        if(CollectionUtils.isNotEmpty(roleList)){
            List<String> roles = roleList.stream().map(UserRoleDO::getName).collect(Collectors.toList());
            userInfoVo.setRoles(roles);
        }
        return userInfoVo;
    }


}
